package org.yoki.tool.core.helper;

import org.yoki.tool.core.bean.BeanTemplateBean;
import org.yoki.tool.core.bean.ColumnTemplateBean;
import org.yoki.tool.core.bean.FieldTemplateBean;
import org.yoki.tool.core.bean.TableTemplateBean;
import org.yoki.tool.core.config.BuilderConfig;
import org.yoki.tool.core.mapper.MySqlMapper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by SKY on 2017/7/23.
 */
public class TemplateChangeHelper {

    private BuilderConfig cfg;

    public TemplateChangeHelper(BuilderConfig cfg) {
        this.cfg = cfg;
    }

    public List<BeanTemplateBean> changeTableList(List<TableTemplateBean> tableList) {
        List<BeanTemplateBean> list = new ArrayList<>();
        for (TableTemplateBean table : tableList)
            list.add(change(table));
        return list;
    }

    public BeanTemplateBean change(TableTemplateBean table) {
        BeanTemplateBean bean = new BeanTemplateBean();
        String tableName = table.getTableName();
        String[] splits = tableName.split("_");
        if (null != splits && splits.length > 0) {
            StringBuffer className = new StringBuffer();
            String moduleName = "a";
            for (int i = 0; i < splits.length; i++) {
                String s = splits[i];
                if (i == 0 && splits.length > 1) {
                    moduleName = s;
                    continue;
                }
                className.append(s.substring(0, 1).toUpperCase() + s.substring(1, s.length()));
            }
            bean.setClassName(className.toString());
            bean.setProjName(cfg.getProjName());
            bean.setProjVersion(cfg.getProjVersion());
            bean.setMavenName(cfg.getMavenName());
            bean.setGroupId(cfg.getGroupId() + "." + cfg.getModuleName());
            bean.setModuleName(moduleName);
        }
        bean.setAuthority("public");
        List<ColumnTemplateBean> columnList = table.getColumnTemplateBeanList();
        for (ColumnTemplateBean column : columnList) {
            FieldTemplateBean field = change(column);
            bean.addField(field);
            if (null != field.getFieldClazz())
                bean.addImport(field.getFieldClazz().getName());
        }
        bean.setTable(table);
        return bean;
    }

    public List<FieldTemplateBean> changeColumnList(List<ColumnTemplateBean> columnList) {
        List<FieldTemplateBean> list = new ArrayList<>();
        for (ColumnTemplateBean column : columnList)
            list.add(change(column));
        return list;
    }

    public FieldTemplateBean change(ColumnTemplateBean column) {
        FieldTemplateBean field = new FieldTemplateBean();
        String columnName = column.getColumnName();
        String[] splits = columnName.split("_");
        if (null != splits && splits.length > 0) {
            StringBuffer fieldName = new StringBuffer();
            for (int i = 0; i < splits.length; i++) {
                String s = splits[i];
                if (i == 0 && splits.length > 1)
                    continue;
                fieldName.append(s.substring(0, 1).toUpperCase() + s.substring(1, s.length()));
            }
            field.setName(fieldName.substring(0, 1).toLowerCase() + fieldName.substring(1, fieldName.length()));
            field.setSetterName("set" + fieldName.toString());
            field.setGetterName("get" + fieldName.toString());
            field.setAuthority("public");
            field.setFieldClazz(MySqlMapper.getJavaType(column.getDataType()));
        }
        field.setColumn(column);
        return field;
    }

}
